﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseModule;

namespace EMC
{
    public class UserManagement
    {
        private DAO dbConn;

        private databaseConfiguration config;

        public UserManagement(databaseConfiguration dbConfig)
        {
            this.config = dbConfig;
            dbConn = dbFactory.newDB(config.dbType, config.dbConnectionString);
        }

        public void adduser(string userid, string first_name, string last_name, string pass)
        {
            KeyValues fieldVals = new KeyValues();

            fieldVals.Add(config.tableUsers_ID, userid);
            fieldVals.Add(config.tableUsers_FirstName, first_name);
            fieldVals.Add(config.tableUsers_LastName, last_name);
            fieldVals.Add(config.tableUsers_Password, pass);

            dbConn.SetVerboseMode(true);
            dbConn.InsertRow(config.tableUsers, fieldVals);
            Console.WriteLine(dbConn.GetLastError());
        }
        public void delUser(string userid)
        {
            KeyValues fieldVals = new KeyValues();

            fieldVals.Add(config.tableUsers_ID, userid);
            dbConn.SetVerboseMode(true);
            dbConn.DeleteRows(config.tableUsers, fieldVals);
        }

        public void setPassword(string userid, string pass)
        {
            KeyValues fieldVals = new KeyValues();
            KeyValues keyVals = new KeyValues();

            keyVals.Add(config.tableUsers_Password, pass);
            fieldVals.Add(config.tableUsers_ID, userid);
            dbConn.SetVerboseMode(true);
            dbConn.UpdateRows(config.tableUsers, keyVals, fieldVals);

        }

        // Assuming that for unentered fields the an empty string is passed
        public void editUser(string userid, string first_name, string last_name, string pass)
        {

            KeyValues fieldVals = new KeyValues();
            KeyValues keyVals = new KeyValues();
            string empty = "";

            if (first_name != empty)
            {

                keyVals.Add(config.tableUsers_FirstName, first_name);

            }
            if (last_name != empty)
            {

                keyVals.Add(config.tableUsers_LastName, last_name);

            }

            if (last_name != empty)
            {

                keyVals.Add(config.tableUsers_Password, pass);

            }

            fieldVals.Add(config.tableUsers_ID, userid);
            dbConn.SetVerboseMode(true);
            dbConn.UpdateRows(config.tableUsers, keyVals, fieldVals);

        }

        public bool userExists(string userid)
        {
            KeyValues fieldVals = new KeyValues();
            fieldVals.Add(config.tableUsers_ID, userid);
            string dbuser = dbConn.GetValue(config.tableUsers, config.tableUsers_ID, fieldVals);
            if (dbuser != null && dbuser.ToLower() == userid.ToLower())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public Dictionary<string, string> GetUser(string userID)
        {
            Dictionary<string, string> userDetais = null;
            if (userExists(userID))
            {
                KeyValues keyVals = new KeyValues();
                keyVals.Add(config.tableUsers_ID, userID);
                userDetais = dbConn.GetRow(config.tableUsers, new string[] { config.tableUsers_ID, config.tableUsers_FirstName, config.tableUsers_LastName }, keyVals);
            }

            //Return the details
            return userDetais;
        }

        public string GetUserPassword(string userID)
        {

            KeyValues keyVals = new KeyValues();
            keyVals.Add(config.tableUsers_ID, userID);

            return dbConn.GetValue(config.tableUsers, config.tableUsers_Password, keyVals);
        
        }


        public List<string> ListUsers()
        {
            List<Dictionary<string, string>> userList = dbConn.GetRows(config.tableUsers, new string[] { config.tableUsers_ID }, null);
            List<string> simplifiedList = userList.ConsolidateValues(config.tableUsers_ID);

            /*
            foreach (KeyValues userDetails in userList)
            {
                try
                {
                    simplifiedList.Add(userDetails[config.tableUsers_ID]);
                }
                catch (Exception)
                {
                    //Something screwed up, ignore it and get on with our life
                }
            }
            */

            return simplifiedList;
        }

        public bool authenticateUser(string userID, string password)
        {
            if (userExists(userID))
            {
                KeyValues keyVals = new KeyValues();
                keyVals.Add(config.tableUsers_ID, userID);
                string userPass = dbConn.GetValue(config.tableUsers, config.tableUsers_Password, keyVals);
                if (password == userPass)
                {
                    return true;
                }
            }

            return false;
        }

    }
}
